package edu.cmu.ece.ds.droidchat.model;

public class ChatMessage implements Comparable<ChatMessage>{

	// the sender
    private UserInfo userinfo;
    
    private String messageContent;
    
    // which chat session does this message belong to
    private String chatSessionID; 

    private int t; // logical clock for the chat session
    private int seqNum; // sequence number from sender
    
    private boolean sysMsg = false;
    
    // only valid when sysMsg is true
    //  if sysMsgEnd==false -> XX is not responsive
    //  if sysMsgEnd==true  -> XX is now responsive
    private boolean sysMsgEnd = false;

	// userinfo is the source of the message
    public ChatMessage(UserInfo userinfo, String messageContent) {
        this.userinfo = userinfo;
        this.messageContent = messageContent;
    }

    public ChatMessage(UserInfo userinfo, String messageContent,
            String chatSessionID) {
        this.userinfo = userinfo;
        this.messageContent = messageContent;
        this.chatSessionID = chatSessionID;
    }

    public UserInfo getUserinfo() {
        return userinfo;
    }

    public void setUserinfo(UserInfo userinfo) {
        this.userinfo = userinfo;
    }

    public String getMessageContent() {
        return messageContent;
    }

    public void setMessageContent(String messageContent) {
        this.messageContent = messageContent;
    }

    public String getChatSessionID() {
        return chatSessionID;
    }

    public void setChatSessionID(String chatSessionID) {
        this.chatSessionID = chatSessionID;
    }

    public void setT(int t){
    	this.t = t;
    }
    public int getT(){
    	return t;
    }
    
    public void setSeqNum(int n){
    	this.seqNum = n;
    }
    public int getSeqNum(){
    	return seqNum;
    }
    
    public void setSystemMsg(boolean b){
    	this.sysMsg = b;
    }
    public boolean isSystemMsg(){
    	return this.sysMsg;
    }
    
    public boolean isSysMsgEnd() {
		return sysMsgEnd;
	}
	public void setSysMsgEnd(boolean sysMsgEnd) {
		this.sysMsgEnd = sysMsgEnd;
	}
    
    @Override
    public String toString() {
        /*return "ChatMessage[userinfo=\"" + userinfo + "\",messageContent=\""
                + messageContent + "]";*/
		return this.userinfo.getUsername() + 
    		" seq=" + this.getSeqNum() + " t=" +this.getT();
    }

	@Override
	public int compareTo(ChatMessage another) {
		
		if(this.t < another.t){
			return -1;
		}else if(this.t > another.t){
			return 1;
		}else{
			if(this.getUserinfo().getUsername().equals(another.getUserinfo().getUsername())){
				if(this.seqNum < another.seqNum){
					return -1;
				}else if(this.seqNum > another.seqNum){
					return 1;
				}else{
					if(this.isSystemMsg() && !another.isSystemMsg()){
						if (this.isSysMsgEnd()==false) {
							return -1;
						} else {
							return 1;
						}
					}
					return 0;
				}
			} else {
				if(this.isSystemMsg() && !another.isSystemMsg()){
					if (this.isSysMsgEnd()==false) {
						return 1;
					} else {
						return -1;
					}
				}
				return 0;
			}
		}
	}

}
